package com.example.Lezedata.ui.fragment.commonfragment;

import android.content.Intent;
import android.os.Bundle;
import android.text.SpannableString;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;

import com.example.Lezedata.R;
import com.example.Lezedata.dataModel.BidDetailManager;
import com.example.Lezedata.ui.activity.pay.PayVc;
import com.example.Lezedata.ui.fragment.BaseDiaLogFragment;
import com.example.Lezedata.util.GBInterface;
import com.example.Lezedata.util.GBUtil;

import bid.Bid;

public class GBBuyGoodsView extends BaseDiaLogFragment {

    public interface EventListener{
        public void buyGoods();
    }
    public GBInterface viewCreatedListener;
    private ImageView buy_goods_icon;
    private TextView buy_goods_name;
    private TextView buy_goods_price;
    private TextView buy_goods_del;
    private TextView buy_goods_input;
    private TextView buy_goods_add;
    private TextView buy_goods_10;
    private TextView buy_goods_20;
    private TextView buy_goods_30;
    private TextView buy_goods_All;
    private TextView buy_goods_number;
    private TextView buy_goods_buy;

    private long buyNumber;

    private long limitNumber;

    private String buyPrice;

    public Bid.BidDetailResponse detailResponse;

    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container,
                             @Nullable Bundle savedInstanceState) {
        return inflater.inflate(R.layout.buy_shop_view, container, false);
    }

    @Override
    public void initView() {
        super.initView();
        findView();
        addAction();
        if (viewCreatedListener != null){
            viewCreatedListener.onViewCreated(contentView);
        }
    }

    void findView(){
        buy_goods_icon = findViewById(R.id.buy_goods_icon);
        buy_goods_name = findViewById(R.id.buy_goods_name);
        buy_goods_price = findViewById(R.id.buy_goods_price);
        buy_goods_del = findViewById(R.id.buy_goods_del);
        buy_goods_input = findViewById(R.id.buy_goods_input);
        buy_goods_add = findViewById(R.id.buy_goods_add);
        buy_goods_10 = findViewById(R.id.buy_goods_10);
        buy_goods_20 = findViewById(R.id.buy_goods_20);
        buy_goods_30 = findViewById(R.id.buy_goods_30);
        buy_goods_All = findViewById(R.id.buy_goods_All);
        buy_goods_number = findViewById(R.id.buy_goods_number);
        buy_goods_buy = findViewById(R.id.buy_goods_buy);
    }

    void addAction(){
        buy_goods_del.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                changeBuyNumber(-1);
            }
        });
        buy_goods_add.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                changeBuyNumber(1);
            }
        });
        buy_goods_10.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                changeBuyNumber(10);
            }
        });
        buy_goods_20.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                changeBuyNumber(20);
            }
        });
        buy_goods_30.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                changeBuyNumber(30);
            }
        });
        buy_goods_All.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                buyAll();
            }
        });
        buy_goods_buy.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                int num = Integer.parseInt(buy_goods_input.getText().toString());
                Intent intent = new Intent(getContext(),PayVc.class);
                intent.putExtra("bidCount",num);
                intent.putExtra("bid",detailResponse.getBidId());
                intent.putExtra("price",buyPrice);
                baseVc().jumpActivityIntent(intent);
                dismiss();
            }
        });
    }

    void buyAll(){
        changeBuyNumber(limitNumber);
    }

    void changeBuyNumber(long number){
        buyNumber += number;
        if (buyNumber < 1){
            buyNumber = 1;
        }
        if (buyNumber > limitNumber){
            buyNumber = limitNumber;
        }
        buy_goods_input.setText(String.valueOf(buyNumber));
        buy_goods_number.setText(String.format("%s/%s",buyNumber,this.detailResponse.getTotalBid()));
        buyPrice = GBUtil.moneyToString(buyNumber * this.detailResponse.getPrice());
        buy_goods_buy.setText(String.format("Buy(%sNGN)",buyPrice));
    }

    public void setDetailResponse(Bid.BidDetailResponse model) {
        this.detailResponse = model;
        limitNumber = model.getRemainBid();
        if (detailResponse.getBidLimit() < model.getRemainBid()){
            limitNumber = detailResponse.getBidLimit();
        }

        BidDetailManager.getGoodsInfoForId(model.getGoodsId(), new BidDetailManager.OnLoadDataComplete() {
            @Override
            public void infoResponse(Bid.GoodsInfo goodsInfo) {
                GBUtil.setGoodsImage(buy_goods_icon,goodsInfo.getImgUrl());
                buy_goods_name.setText(goodsInfo.getName());
            }
        });

        String price = String.valueOf(model.getPrice());
        String str = price + "ter pid";
        SpannableString string = GBUtil.textAddColorAndFont(str,getResources().getColor(R.color.app_color_main),25,0,price.length());
        buy_goods_price.setText(string);
        changeBuyNumber(1);
    }


    @Override
    public int windowAnimationsType() {
        return R.style.popWindow_anim_style;
    }
}
